/*
 *
 *  Copyright (C) 2008-2010 GSyC/LibreSoft, Universidad Rey Juan Carlos
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@libresoft.es>
 *
 */

package com.LibreGeoSocial.App.AlarmManager;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.LibreGeoSocial.Api.Audio;
import com.LibreGeoSocial.Api.GeoNode;
import com.LibreGeoSocial.Api.Note;
import com.LibreGeoSocial.Api.Photo;
import com.LibreGeoSocial.Api.User;
import com.LibreGeoSocial.App.AudioCard;
import com.LibreGeoSocial.App.NoteCard;
import com.LibreGeoSocial.App.PhotoCard;
import com.LibreGeoSocial.App.R;
import com.LibreGeoSocial.App.UserCard;

public class AlarmList extends ListActivity {

	// Context menu
	private static final int MENU_DELETE_ALARM = Menu.FIRST + 1;
	
	private AlarmAdapter mAdapter; 
	protected static ArrayList<GenericAlarm> mAlarmList ;
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		mAlarmList = AlarmService.getAlarms();
		
		// Adapter for ListView
        mAdapter = new AlarmAdapter(this);
        setListAdapter(mAdapter);
        
        if ( mAlarmList.size()==0 )
        {
        	
    		Toast.makeText(getBaseContext(), 
    					   "Currently, you haven't got alarms created", 
    					   Toast.LENGTH_LONG).show();        	
        }
        
        registerForContextMenu(this.getListView());
        
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View view,  ContextMenuInfo menuInfo)
	{ 
		menu.add (0, MENU_DELETE_ALARM, 0, "Delete Alarm");
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) 
	{
		
		
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		GenericAlarm alarm = (GenericAlarm) mAlarmList.get(info.position);
		
		switch (item.getItemId()) {
		
			case MENU_DELETE_ALARM:
				AlarmService.unregisterAlarm(info.position+1);
				setListAdapter(mAdapter);
		}
		
		return true;
	}
	
    protected void onListItemClick(ListView l, View v, int position, long id)
    {

    	GenericAlarm myAlarm = mAlarmList.get(position);
    	
    	 if ( NodeAlarm.class.isInstance(myAlarm) )
         {
         	NodeAlarm nAlarm = (NodeAlarm) myAlarm;
         	GeoNode myResource = (GeoNode) nAlarm.getNode();
         	

        	if ( Photo.class.isInstance(myResource) )
        	{
    	        Intent i = new Intent(this, PhotoCard.class);
    	        i.putExtra("node",myResource);
    	        
    	        startActivity(i);
        	}
        	else if ( Note.class.isInstance(myResource) )
        	{
    	        Intent i = new Intent(this, NoteCard.class);
    	        i.putExtra("node",myResource);
    	        
    	        startActivity(i);
        	}
        	else if ( User.class.isInstance(myResource) )
        	{
    	        Intent i = new Intent(this, UserCard.class);
    	        i.putExtra("node",myResource);
    	        
    	        startActivity(i);
        	}
        	else if ( Audio.class.isInstance(myResource) )
        	{
    	        Intent i = new Intent(this, AudioCard.class);
    	        i.putExtra("node",myResource);
    	        
    	        startActivity(i);
        	}
         }

    }

    public static class AlarmAdapter extends BaseAdapter
    {

        private Context mContext;

        public AlarmAdapter(Context c)
        {
            mContext = c;
        }

        public int getCount()
        {
            // TODO Auto-generated method stub
            return mAlarmList.size();
        }

        public Object getItem(int position)
        {
            // TODO Auto-generated method stub
            return position;
        }

        public long getItemId(int position)
        {
            // TODO Auto-generated method stub
            return position;
        }
        
        public View getView(int position, View convertView, ViewGroup parent)
        {
            // TODO Auto-generated method stub
            View view;

            if (convertView == null)
            {
                // Make up a new view
                LayoutInflater inflater = (LayoutInflater) mContext
                                          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.generic_list, null);
            }
            else
            {
                // Use convertView if it is available
                view = convertView;
            }


            ImageView img = (ImageView) view.findViewById(R.id.image);

            GenericAlarm myAlarm = mAlarmList.get(position);
            String title = null;
            String description = null;

            if ( NodeAlarm.class.isInstance(myAlarm) )
            {
            	NodeAlarm nAlarm = (NodeAlarm) myAlarm;
            	GeoNode myResource = (GeoNode) nAlarm.getNode();
            	
	            if ( Photo.class.isInstance(myResource) )
	            {
	                Photo myphoto = (Photo) myResource;
	                img.setImageBitmap(myphoto.getBitmapImageThumb());
	                title = myphoto.getName();
	            }
	            else if (Note.class.isInstance(myResource) )
	            {
	                Note mynote = (Note) myResource;
	                img.setImageResource(R.drawable.paper_pencil_48);
	                title = mynote.getTitle();
	            }
	            else if ( User.class.isInstance(myResource))
	            {
	                User myuser = (User) myResource;
	                img.setImageResource(R.drawable.user_48);
	                title = myuser.getName() + " (" + myuser.getUsername() +")";
	            }
	            else if (Audio.class.isInstance(myResource) )
	            {
	                Audio mysound = (Audio) myResource;
	                img.setImageResource(R.drawable.audio_48);
	                title = mysound.getName();
	            }

	            description = "Notify when is less than " + String.valueOf(nAlarm.getDistance()/1000) + " Km.";
	            	
	            TextView t = (TextView) view.findViewById(R.id.title);
	            t.setText(title);

	            t = (TextView) view.findViewById(R.id.owner);
	            t.setText(description);

	           
	            
            }

            return view;
            
        }

    }

}
